<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="消息详情" @ok="handleSubmit">
    <Description @register="registerDescription" class="mt-4" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, h } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { Description, DescItem, useDescription } from '/@/components/Description/index';
  import { getMessageDetail } from '/@/api/system/system';
  const schema: DescItem[] = [
    {
      field: 'formUser',
      label: '发送人',
    },
    {
      field: 'createTime',
      label: '发送时间',
    },
    {
      field: 'subject',
      label: '标题',
      span: 2,
    },
    {
      field: 'content',
      label: '内容',
      span: 2,
      render: (curVal) => {
        return h('p', { innerHTML: curVal });
      },
    },
  ];

  export default defineComponent({
    name: 'MessageDetailModal',
    components: {
      BasicModal,
      Description,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const data: Recordable = ref({});

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async ({ record }) => {
        setModalProps({ confirmLoading: true, maskClosable: false, width: 750 });
        const detailMessage = await getMessageDetail(record.id);
        record.content = detailMessage.content;
        data.value = record;
        setModalProps({ confirmLoading: false });
      });

      const [registerDescription] = useDescription({
        column: 2,
        data: data,
        schema: schema,
      });

      /**********************表单提交*************************/

      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });
          closeModal();
          emit('success');
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        handleSubmit,
        registerDescription,
      };
    },
  });
</script>
